package com.zhiyou.crowdfunding.controller;


import com.zhiyou.crowdfunding.bean.Admin;
import com.zhiyou.crowdfunding.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jyf
 * @since 2022-05-27
 */
@CrossOrigin
@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    /**
     * 登录
     */
    @RequestMapping("/toLogin")
    public String toLogin(@RequestParam("username")String adminName, @RequestParam("password")String adminPassword, HttpSession session){
        Admin admin=new Admin();
        admin.setAdminName(adminName);
        admin.setAdminPassword(adminPassword);

        System.out.println(adminName);
        System.out.println(adminPassword);
        Admin admin1 = adminService.toLogin(admin);
        if (admin1==null){
            System.out.println("=====error===");
            session.setAttribute("msg","用户名密码错误！");
            return "error";
        }
        session.setAttribute("admin",admin1);
        return "success";
    }
    @RequestMapping("/login")
    public String login(@RequestBody  Admin admin){

        System.out.println(admin.getAdminName()+"++"+admin.getAdminPassword());
        System.out.println("cccc");
        return "success";
    }



    /**
     * 查询所有管理员
     * @return
     */
    @GetMapping("/getAll")
    @ResponseBody
    public List<Admin> getAll(){
        return adminService.getAll();
    }

}

